package edu.tcc.iot.stub.controller;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Date;
import edu.tcc.iot.domain.ConnectionInfo;
import edu.tcc.iot.domain.vo.ProductInfoVO;
import edu.tcc.iot.domain.vo.ProductVO;
import edu.tcc.iot.domain.vo.ShelfVO;
import edu.tcc.iot.stub.view.View;

public class Controller {

	View view = null;
	private ObjectOutputStream oos;
	private long time = 20000;
	
	ProductInfoVO bananaVO = new ProductInfoVO("Banana Oura", 900, 1.50, new Date(2013, 02, 07));
	ProductVO banana = new ProductVO(1, bananaVO);
	ProductInfoVO cocacolaVO = new ProductInfoVO("Coca Cola", 45, 2.00, new Date(2050, 01, 01));
	ProductVO cocacola = new ProductVO(12, cocacolaVO);
	ProductInfoVO chocolateVO = new ProductInfoVO("Chocolate Nestle", 100, 4.00, new Date(2013, 02, 06));
	ProductVO chocolate = new ProductVO(123, chocolateVO);
	
	ProductInfoVO morangoVO = new ProductInfoVO("Morango", 400, 5.00, new Date(2020, 02, 07));
	ProductVO morango = new ProductVO(4, morangoVO);
	ProductInfoVO guaranaVO = new ProductInfoVO("Guarana", 45, 2.00, new Date(2050, 01, 01));
	ProductVO guarana = new ProductVO(45, guaranaVO);
	ProductInfoVO bolachaVO = new ProductInfoVO("Bolacha", 100, 4.00, new Date(2080, 02, 06));
	ProductVO bolacha = new ProductVO(456, bolachaVO);
	

	public boolean connect(){
		try {
			ConnectionInfo info = view.getConnectionInfo();
			Socket socket = new Socket(info.getServerIP(), info.getServerPort());
			oos = new ObjectOutputStream(socket.getOutputStream());
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public void setView(View view) {
		this.view = view;
	}

	public void sendShelf(){
		try {
			ShelfVO stub = new ShelfVO(view.getConnectionInfo().getShelfID());
			oos.writeObject(stub);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void sendProductsShelf1(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true){

					try {
						synchronized (oos) {
							oos.writeObject(banana);
							oos.writeObject(cocacola);
							oos.writeObject(chocolate);
						}
						Thread.sleep(time);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	public void sendProductsShelf2(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true){

					try {
						synchronized (oos) {
							oos.writeObject(morango);
							oos.writeObject(guarana);
							oos.writeObject(bolacha);
						}
						Thread.sleep(time);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

}
